<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\signup\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\signup\model\signup;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 报名列表逻辑层
 */
class signupLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new signup();
    }
    //我的活动
    public function getSignupList($userId,$limit){
      
          return $this->model
            ->Join('eb_activity', 'eb_signup.activity_id = eb_activity.activity_id')
             ->field('eb_activity.activity_id,eb_activity.activity_title,eb_activity.activity_address,eb_activity.activity_cover,eb_activity.create_time,eb_activity.activity_start_time,eb_activity.activity_end_time')
            ->where('eb_signup.user_id', $userId)
             ->where('eb_activity.is_display', 1)
            // 修正拼写错误，将 ceeate_time 改为 create_time
            ->order('eb_signup.create_time desc')
            ->paginate($limit);
    }
    public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);
       
        $file_name = '报名数据.xlsx';
        $header = [ '活动', '会员姓名','性别', '手机号码','公司名称',
        '公司职位','报名时间','创建时间'];
       
        $filter = [
            // 'activity_status' => [
            //     ['value' => 0, 'label' => '进行中'],
            //     ['value' => 1, 'label' => '已结束'],
            //     ['value' => 2, 'label' => '未开始']
            // ]
        ];
        $data_attr=[];
        foreach($data as $value){
           $data_attr[] = [
            'activity_id_text'=>$value['activity_id_text'],
            'member_name'=>$value['member_name'],
            'sex'=>$value['sex']==1?'男':'女',
            'phone_number'=>$value['phone_number'],
            'company_name'=>$value['company_name'],
            'company_position'=>$value['company_position'],
            'signup_time'=>$value['signup_time'],
            'create_time'=>$value['create_time']
           
           ];
        }

        $writer = new OpenSpoutWriter($file_name);
        $writer->setWidth([20, 20, 20, 20, 20, 20,20,20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }

}
